<?php

/**
 * Created by PhpStorm.
 * User: steven
 * Date: 2016/5/2
 * Time: 19:31
 */
class Expose_Model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

//    查询用户列表


    public function get_expose($data){
        $sql="select * from Repudiation_Personal";
        $sql_clause="";
        if(!empty($data['ID'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." ID='".$data['ID']."'";
            }else{
                $sql_clause=$sql_clause." AND ID='".$data['ID']."'";
            }
        }
        if(!empty($data['PublishDate'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." PublishDate='".$data['PublishDate']."'";
            }else{
                $sql_clause=$sql_clause." AND PublishDate='".$data['PublishDate']."'";
            }
        }

        if(!empty($data['CauseNo'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." CauseNo='".$data['CauseNo']."'";
            }else{
                $sql_clause=$sql_clause." AND CauseNo='".$data['CauseNo']."'";
            }
        }

        if(!empty($data['Cause'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." Cause='".$data['Cause']."'";
            }else{
                $sql_clause=$sql_clause." AND Cause='".$data['Cause']."'";
            }
        }
        if(!empty($data['RealName'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." RealName='".$data['RealName']."'";
            }else{
                $sql_clause=$sql_clause." AND RealName='".$data['RealName']."'";
            }
        }
        if(!empty($data['Address'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." Address='".$data['Address']."'";
            }else{
                $sql_clause=$sql_clause." AND Address='".$data['Address']."'";
            }
        }

        if(!empty($data['Pic'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." Pic='".$data['Pic']."'";
            }else{
                $sql_clause=$sql_clause." AND Pic='".$data['Pic']."'";
            }
        }

        if(!empty($data['IDNumber'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." IDNumber='".$data['IDNumber']."'";
            }else{
                $sql_clause=$sql_clause." AND IDNumber='".$data['IDNumber']."'";
            }
        }

        if(!empty($data['Contact'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." Contact='".$data['Contact']."'";
            }else{
                $sql_clause=$sql_clause." AND Contact='".$data['Contact']."'";
            }
        }

        if(!empty($data['Intro'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." Intro='".$data['Intro']."'";
            }else{
                $sql_clause=$sql_clause." AND Intro='".$data['Intro']."'";
            }
        }
        if(!empty($data['Amount'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." Amount='".$data['Amount']."'";
            }else{
                $sql_clause=$sql_clause." AND Amount='".$data['Amount']."'";
            }
        }
        if(!empty($data['Reward'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." Reward='".$data['Reward']."'";
            }else{
                $sql_clause=$sql_clause." AND Reward='".$data['Reward']."'";
            }
        }

        if(!empty($data['ClickNum'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." ClickNum='".$data['ClickNum']."'";
            }else{
                $sql_clause=$sql_clause." AND ClickNum='".$data['ClickNum']."'";
            }
        }
        if(!empty($data['Status'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." Status='".$data['Status']."'";
            }else{
                $sql_clause=$sql_clause." AND Status='".$data['Status']."'";
            }
        }
        if(!empty($data['OprTime'])){
            if(empty($sql_clause)){
                $sql_clause=$sql_clause." OprTime='".$data['OprTime']."'";
            }else{
                $sql_clause=$sql_clause." AND OprTime='".$data['OprTime']."'";
            }
        }

//        if(isset($data['sort'])){
//            $sql=$sql." order by ".$data['sort'];
//        }
//        if(isset($data['start_row'])){
//            $sql=$sql." limit ".$data['start_row'].",".$data['number'];
//        }
        if(!empty($sql_clause)){
            $sql=$sql.' where'.$sql_clause;
        }
        $sql=$sql.$sql_clause;
        $query=$this->db->query($sql);
        return $query->result_array();
    }

//    根据id查询问题
    public function get_expose_by_id($id){
        $query = $this->db->get_where('Repudiation_Personal', array('id' => $id));
        $qr=$query->result_array();
        if($qr){
            return $qr[0];
        }else{
            return null;
        }
    }

    public function get_all_expose_count(){
        $sql='select count(*) as total from Repudiation_Personal where 1';
        $query=$this->db->query($sql);
        if ($query->num_rows() > 0){
            return $query->row()->total;
        }else{
            return 0;
        }

    }

    public function get_expose_by_page($pagesize,$endrow){
        $sql="SELECT * FROM Repudiation_Personal w1, ( SELECT TOP ".$pagesize." ID FROM (SELECT TOP ".$endrow.
            " ID, PublishDate FROM Repudiation_Personal ORDER BY PublishDate DESC, ID DESC) w ORDER BY
            w.PublishDate ASC, w.ID ASC w2 WHERE w1.ID = w2.ID ORDER BY w1.PublishDate DESC, w1.ID DESC"  ;
        $query=$this->db->query($sql);
        return $query->result_array();
    }
}